package com.mingqi.enetconnected.net.request;

import android.content.Context;

import com.google.gson.reflect.TypeToken;
import com.mingqi.enetconnected.net.model.app.CheckVersionInfo;
import com.mingqi.enetconnected.net.model.randomcode.RandomCode;

import java.lang.reflect.Type;
import java.util.HashMap;

/**
 * 12202 RandomCodeRemoteService
 * 共2个，已完成
 */
public class RandomCodeRequest {
    /**
     * 创建串码/获取串码
     */
    public final static String RANDOM_CODE_URL = "/v1/plat/randomCode";

    /**
     * 创建串码
     * @param cxt
     * @param type      串码类型
     * @param expiresIn 串码有效期，单位为秒
     * @param content  串码值(户号)
     */
    public static RandomCode createRandomCode(Context cxt, String type, int expiresIn, String content) throws Exception {
        Type types = new TypeToken<CheckVersionInfo>() {
        }.getType();
        Communicator<RandomCode> communicator = new Communicator<>(cxt, RANDOM_CODE_URL);
        HashMap<String, String> params = new HashMap<>();
        params.put("type", type);
        params.put("expiresIn", expiresIn + "");
        params.put("content", content);
        return communicator.post(params, types);
    }

    /**
     * 获取串码
     * @param cxt
     * @param code  32位的随机串码值
     * @return
     * @throws Exception
     */
    public static String getRandomCode(Context cxt, String code) throws Exception {
        Type types = new TypeToken<String>() {
        }.getType();
        Communicator<String> communicator = new Communicator<>(cxt, RANDOM_CODE_URL + "/" + code);
        return communicator.get(types);
    }

}
